#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glob.h>
#include <string.h>


#define DELIMS		" \t\n"

struct cmd_st
{
	glob_t globres;
};

static void prompt(void)
{
	printf("mysh-0.1$ ");
}

void parse(char *line,struct cmd_st *res)
{
//	 ls   -l /etc      -a    -i   
	char *tok;	
	glob_t globres;
	int i = 0;

	while(1)
	{
		tok = strsep(&line,DELIMS);
		if(tok == NULL)
			break;
		glob(tok,GLOB_NOCHECK|GLOB_APPEND*i,NULL,&res->globres);
		i = 1;
	}
}

int main()
{// ls -l -a -i /etc

	char *linebuf = NULL;
	size_t linesize = 0;
	struct cmd_st cmd;
	pid_t pid;

	while(1)
	{
		prompt();

		if(getline(&linebuf,&linesize,stdin) < 0)
			break;

		parse(linebuf,&cmd);

		if(0 /*内部命令*/)
		{ 	

		}
		else	//外部命令
		{
			pid = fork();
			if(pid < 0)	
			{
				perror("fork()");
				break;
			}

			if(pid == 0)		// child
			{
				execvp(cmd.globres.gl_pathv[0],cmd.globres.gl_pathv);
				perror("execvp()");
				exit(1);
			}
			else			// parent
				wait(NULL);
		}
	}

}



